﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyGeneration.dOOdads;
using System.Data;
using POSSystem.BLL;
using System.Globalization;

namespace WebUI.Controls
{
    public partial class CustomerOrders : BaseListControl
    {
        public View_CustomerOrders ViewOrders
        {
            get { return (View_CustomerOrders)SourceObject; }
            set { SourceObject = value; }
        }

        public int DumpID
        {
            get
            {
                if (ViewState[ClientID + "_DumpID"] == null || !(ViewState[ClientID + "_DumpID"] is int))
                    ViewState[ClientID + "_DumpID"] = 0;
                return (int)ViewState[ClientID + "_DumpID"];
            }
            set { ViewState[ClientID + "_DumpID"] = value; }
        }

        public string GetStatusImageLink(int statusID)
        {
            if (statusID == OrderStatus.Pending)
                return OrderStatus.Pending_Link;
            if (statusID == OrderStatus.Rejected)
                return OrderStatus.Rejected_Link;
            if (statusID == OrderStatus.InvoiceCreated)
                return OrderStatus.InvoiceCreated_Link;
            if (statusID == OrderStatus.Shipped)
                return OrderStatus.Shipped_Link;

            return "";
        }

        public string GetStatusImageDesc(int statusID)
        {
            if (statusID == OrderStatus.Pending)
                return "Pending";
            if (statusID == OrderStatus.Rejected)
                return "Rejected";
            if (statusID == OrderStatus.InvoiceCreated)
                return "Invoice Created";
            if (statusID == OrderStatus.Shipped)
                return "Shipped";
            
            return "";
        }

        public void DeleteCheckedItems()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);


            if (checkedItemID != "")
            {
                POSSystem.BLL.CustomerOrders order = POSSystem.BLL.CustomerOrders.GetOrdersByIds(checkedItemID);
                for (int i = 0; i < order.RowCount; i++)
                {
                    order.Deleted = true;
                    order.MoveNext();
                }
                order.Save();
            }
        }
        public string PrintKey
        {
            get
            {
                if (ViewState["PrintKey"] == null)
                    ViewState["PrintKey"] = Guid.NewGuid().ToString();
                return ViewState["PrintKey"].ToString();
            }
        }

        public string GetPrintPage()
        {
            return this.Page.ResolveUrl("~/EditPages/PrintHandler.aspx?key=" + PrintKey);
        }

        protected void btnPrint_OnClick(object sender, EventArgs e)
        {
            PrintInvoice();
        }

        private void PrintInvoice()
        {
            try
            {
                InvoiceViewerCtrl ivcViewer = (InvoiceViewerCtrl)LoadControl(@"InvoiceViewerCtrl.ascx");
                if (hidInvoiceID.Value != "")
                    ivcViewer.LoadInvoiceByID(int.Parse(hidInvoiceID.Value));

                Session[PrintKey] = ivcViewer;
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showPrintDlg('{0}');", GetPrintPage()), true);

                Refresh();
            }
            catch (Exception ex)
            {
                ErrorLog.WriteToFile("CustomerOrders.ascx.cs", "PrintInvoice", ex.StackTrace);
            }
        }
    }
}